package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.StringUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.InfoBirthResult;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.PhotoUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.AndroidDateDialog;
import richbar.com.richbar.view.dialog.InputDialog;
import richbar.com.richbar.view.dialog.SingleSelectDialog;

/**
 * Created by QiYang on 2018/10/31.
 */

public class PersonInfoAc extends BaseActivity {
    private LoadingDialog loadingDialog;
    @BindView(R.id.head_iv)
    ImageView head_iv;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.sex)
    TextView sex_tv;
    @BindView(R.id.date)
    TextView date;
    private String path = "";

    @Override
    public int initLayout() {
        return R.layout.ac_person_info;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("个人信息");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(PersonInfoAc.this, 0);
    }

    @Override
    public void initData() {
        getUserInfo();
    }


    private void getUserInfo() {
        loadingDialog.show();
        NetWorkUtil.getInfo(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final UserInfoResult result = MyGson.getInstance().fromJson(data, UserInfoResult.class);
                if (checkIsOk(result)) {
                    setUserInfo(result);
                    loadingDialog.dismiss();
                }
            }
        });
    }

    private void setUserInfo(UserInfoResult result) {
        UserInfoResult.DataEntity.InfoEntity bean = result.getData().getInfo();
        username.setText(bean.getUsername());
        nickname.setText(StringUtil.isEmpty(bean.getNickname()) ? "" : bean.getNickname());
        sex_tv.setText(bean.getSex() == 1 ? "男" : "女");
        date.setText(bean.getBirth());
        GlideUtil.loadImage(PersonInfoAc.this, bean.getHead(), head_iv);
    }


    @OnClick({R.id.head_iv, R.id.nickname, R.id.sex, R.id.date})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_iv:
                PhotoUtil.openPhotoSingle(this);
                setResult();
                break;
            case R.id.nickname:
                showNickname();
                setResult();
                break;
            case R.id.sex:
                showSex();
                break;
            case R.id.date:
                showDate();
                break;
        }
    }

    public void setResult() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
    }

    private void showNickname() {
        InputDialog.show(this, "请填写昵称", new InputDialog.Callback() {
            @Override
            public void onBesure(String s) {
                changeInfoNickname(s);
            }
        });
    }

    private void showSex() {
        SingleSelectDialog dialog = new SingleSelectDialog(this);
        String[] s = {"男", "女"};
        dialog.showSingleDialog("请选择", Arrays.asList(s), new SingleSelectDialog.OnSelectListener() {
            @Override
            public void onSelect(String s, int pos) {
                changeInfoSex(s);
            }
        });
    }

    private void showDate() {
        AndroidDateDialog dateDialog = new AndroidDateDialog();
        dateDialog.show(this, new AndroidDateDialog.Callback() {
            @Override
            public void onDate(String d) {
                changeInfoDate(d);
            }
        });
    }


    private void changeInfoHead() {
        loadingDialog.show();
        NetWorkUtil.editInfoHead(Util.imageToBase64(path), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final InfoBirthResult result = MyGson.getInstance().fromJson(data, InfoBirthResult.class);
                if (checkIsOk(result)) {
                    if (result.getData().getResult() == 1) {
                        loadingDialog.dismiss();
                        GlideUtil.loadImage(PersonInfoAc.this, path, head_iv);
                    }
                }
            }
        });
    }

    private void changeInfoNickname(final String s) {
        loadingDialog.show();
        NetWorkUtil.editInfoNickname(s, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final InfoBirthResult result = MyGson.getInstance().fromJson(data, InfoBirthResult.class);
                if (checkIsOk(result)) {
                    if (result.getData().getResult() == 1) {
                        loadingDialog.dismiss();
                        nickname.setText(s);
                    }
                }
            }
        });
    }


    private void changeInfoDate(final String d) {
        loadingDialog.show();
        NetWorkUtil.editInfoBirth(d, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final InfoBirthResult result = MyGson.getInstance().fromJson(data, InfoBirthResult.class);
                if (checkIsOk(result)) {
                    if (result.getData().getResult() == 1) {
                        loadingDialog.dismiss();
                        date.setText(d);
                    }
                }
            }
        });
    }

    private void changeInfoSex(final String s) {
        loadingDialog.show();
        String sex = s.equals("男") ? "1" : "2";
        NetWorkUtil.editInfoSex(sex, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final InfoBirthResult result = MyGson.getInstance().fromJson(data, InfoBirthResult.class);
                if (checkIsOk(result)) {
                    if (result.getData().getResult() == 1) {
                        loadingDialog.dismiss();
                        sex_tv.setText(s);
                    }
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> list = PictureSelector.obtainMultipleResult(data);
            if (list.size() > 0) {
                path = list.get(0).getCompressPath();
                changeInfoHead();
            }

        }
    }
}
